package com.zclcs.config;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.UnauthenticatedException;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.support.spring.FastJsonJsonView;

/**
 * 爆发异常的处理
 * 
 * @author 周成龙
 *
 */
public class MyExceptionHandler implements HandlerExceptionResolver {

	@Override
	public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler,
			Exception ex) {
		ex.printStackTrace();
		ModelAndView mv = new ModelAndView();
		FastJsonJsonView view = new FastJsonJsonView();
		Map<String, Object> attributes = new HashMap<String, Object>();
		if (ex instanceof UnauthenticatedException) {
			attributes.put("code", 401);
			attributes.put("message", "token错误");
			attributes.put("success", "false");
		} else if (ex instanceof UnauthorizedException) {
			attributes.put("code", 402);
			attributes.put("message", "用户无权限");
			attributes.put("success", "false");
		} else {
			attributes.put("code", 9999999);
			attributes.put("message", ex.getMessage());
			attributes.put("success", "false");
		}
		view.setAttributesMap(attributes);
		mv.setView(view);
		return mv;
	}
}